package com.ke.controller;

import com.ke.pojo.BaseResp;
import com.ke.pojo.User;
import com.ke.service.UserService;
import com.ke.utils.JwtUtils;
import com.ke.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author bochen
 */
@CrossOrigin(allowCredentials = "true",allowedHeaders ="*")
@RestController
@RequestMapping("/user")
public class UserController {
    private UserService userService;
    @Autowired
    public void setUserService(UserService userService){
        this.userService=userService;
    }
    @RequestMapping("/validateUsername")
    public BaseResp validateUsername(@RequestBody Map<String,String> map){
        String username = map.get("username");
        return userService.validateUsername(username);
    }
    @RequestMapping("/validateEmail")
    public BaseResp validateEmail(@RequestBody Map<String, String> map){
        String email = map.get("email");
        return userService.validateEmail(email);
    }
    @RequestMapping("/sendMail")
    public BaseResp sendMail(@RequestBody Map<String, String> map){
        String email = map.get("email");
        return userService.sendMail(email);
    }
    @RequestMapping("/registry")
    public BaseResp registry(@RequestBody User user){
        return userService.registry(user);
    }
    @RequestMapping("/login")
    public BaseResp login(@RequestBody User user){
        return userService.login(user);
    }
    @RequestMapping("/findUser")
    public BaseResp findUser(HttpServletRequest request){
        String username = (String) request.getAttribute("username");
        return userService.findUser(username);
    }
    @RequestMapping("/uploadPic")
    public String uploadPic(MultipartFile file){
        return UploadUtil.upload(file);
    }
    @RequestMapping("/updateUser")
    public BaseResp updateUser(@RequestBody User user){
        return userService.updateUser(user.getId());
    }

    /**
     * 模糊查询
     * @param map
     * @return
     */
    @RequestMapping("/look")
    public BaseResp look(@RequestBody Map map){
        return userService.look(map.get("word").toString());
    }

    @RequestMapping("/findUserById")
    public BaseResp findUserById(@RequestBody Map map){
        return userService.findUserById(Integer.valueOf(map.get("id").toString()));
    }
}
